package com.qfedu.controller;

        import com.alibaba.dubbo.config.annotation.Reference;
        import com.github.pagehelper.PageInfo;
        import com.qfedu.entity.*;
        import com.qfedu.kit.StringUtil;
        import com.qfedu.service.FreigntService;
        import com.qfedu.service.RouteService;
        import com.qfedu.service.UserService;
        import org.springframework.stereotype.Controller;
        import org.springframework.web.bind.annotation.RequestMapping;
        import org.springframework.web.bind.annotation.ResponseBody;

        import javax.servlet.http.HttpServletRequest;
        import java.util.ArrayList;
        import java.util.List;

@Controller
public class FreightController {

    @Reference(check = false)
    FreigntService freigntService;

    @Reference(check = false)
    UserService userService;

    @Reference(check = false)
    RouteService routeService;


    //跳转到信息查询与订阅
    @RequestMapping("jumpcargo")
    public String jumpcargo(){
        return "GoodsTrack";
    }


    //跳转滞期费查询假面
    @RequestMapping("jumpdemu")
    public String jumpzhqi(){
        return "demurrage";
    }

    //跳转方箱查询界面
    @RequestMapping("jumpbox")
    public String jumpbox(){
        return "boxSide";
    }




    //查询货物跟踪根据订单id
    @RequestMapping("queryorderbyid")
    public String queryorder(String oid, HttpServletRequest request){
        System.out.println(oid);
        //货物追踪订单

        if (StringUtil.isEnpty(oid)){
            return "GoodsTrack";
        }
        UserEntity user = (UserEntity) request.getSession().getAttribute("user");
        if (user!=null){


            FreightEntity freight = freigntService.querycargo(oid);
            //查找航线
            RouteEntity routeEntity = routeService.queryRouteById(freight.getRid());

            //查找船运,这里就虚拟一个船id进行测试

            if (freight==null) {
                return "GoodsTrack";
            }
            BoatEntity boat = freigntService.queryboat(freight.getSid());
            System.out.println(boat);
            //根据船id查询集装箱
            List<BoxEntity> box = freigntService.querybox(freight.getSid());


            String id = "";
            String type = "";
            String  size = "";
            String  num = "";
            for (BoxEntity bu : box){
                id+=bu.getBid()+",";
                type+=bu.getBtype()+",";
                size+=bu.getBsize()+"立方,";
                num+=bu.getBnum()+"个";
            }

            System.out.println(box);

            freight.setBid(id);
            freight.setBnum(num);
            freight.setBtype(type);
            freight.setBsize(size);
            freight.setSname(boat.getSname());
            freight.setBox(box);
            freight.setRname(routeEntity.getRname());
            freight.setSplace(routeEntity.getSplace());
            freight.setAplace(routeEntity.getAplace());
            request.setAttribute("freight",freight);
            System.out.println(freight);


            return "GoodsTrack";
        }else {
            return "login";
        }
    }





    //订单滞期港口费查询
    @RequestMapping("queryboxbyid")
    public String queryboxbyid(String oid,HttpServletRequest request){

        if (StringUtil.isEnpty(oid)){
            return "demurrage";
        }
        UserEntity user = (UserEntity) request.getSession().getAttribute("user");
        if (user!=null) {
            UserEntity userEntity = new UserEntity();
            FreightEntity freightEntity = freigntService.querycargo(oid);
            if (freightEntity==null){
                return "demurrage";
            }
            userEntity.setUid(freightEntity.getUid());
            UserEntity user1 = userService.queryuser(userEntity);

            //根据船id查询集装箱
            List<BoxEntity> boxlist = freigntService.querybox(freightEntity.getSid());

            String bid = "";
            String btype = "";
            String bsize = "";
            for (BoxEntity box : boxlist) {
                bid += box.getBid() + ",";
                btype += box.getBtype() + ",";
                bsize += box.getBsize() + "立方,";
            }


            //根据船查询船
            BoatEntity soat = freigntService.queryboat(freightEntity.getSid());

            //根据船类别查询类别
            BoatTypeEntity boattype = freigntService.queryboattype(soat.getSoid());

            ZhiqiEntity zhiqiEntity = new ZhiqiEntity();
            zhiqiEntity.setOid(freightEntity.getOid());
            zhiqiEntity.setOprice(freightEntity.getOprice());
            zhiqiEntity.setSname(soat.getSname());
            zhiqiEntity.setScountry(soat.getScountry());
            zhiqiEntity.setSoname(boattype.getSoname());
            zhiqiEntity.setBid(bid);
            zhiqiEntity.setBtype(btype);
            zhiqiEntity.setBsize(bsize);
            zhiqiEntity.setUname(user1.getUname());
            request.getSession().setAttribute("memb", zhiqiEntity);

            return "demurrage";
        }else {
            return "login";
        }
    }


    @RequestMapping("querybox")
    public String querybox(String oid,HttpServletRequest request){

        if (StringUtil.isEnpty(oid)){
            return "boxSide";
        }
        UserEntity user = (UserEntity) request.getSession().getAttribute("user");
        if (user!=null){


            FreightEntity freight = freigntService.querycargo(oid);
            //查找航线
            //RouteEntity routeEntity = routeService.queryOrderByid(freight.getRid());

            if (freight==null) {
                return "GoodsTrack";
            }
            //查找船运,这里就虚拟一个船id进行测试
            BoatEntity boat = freigntService.queryboat(freight.getSid());
            System.out.println(boat);
            //根据船id查询集装箱
            List<BoxEntity> box = freigntService.querybox(freight.getSid());

            String id = "";
            String type = "";
            String  size = "";
            String  num = "";
            for (BoxEntity bu : box){
                id+=bu.getBid()+",";
                type+=bu.getBtype()+",";
                size+=bu.getBsize()+"立方,";
                num+=bu.getBnum()+"个";
            }

            System.out.println(box);

            freight.setBid(id);
            freight.setBnum(num);
            freight.setBtype(type);
            freight.setBsize(size);
            freight.setSname(boat.getSname());
            freight.setBox(box);

            request.setAttribute("freight",freight);
            System.out.println(freight);


            return "boxSide";
        }else {
            return "login";
        }
    }



    //跳转到评价管理界面并做分页
    @RequestMapping("jumpestimate")
    public String jumpestimate(OrderEntity orderEntity,HttpServletRequest request){

        PageInfo<OrderEntity> PageInfo = freigntService.querymeate(orderEntity);

        request.getSession().setAttribute("PageInfo",PageInfo);

        return "estimate";
    }

}
